#include "mainbuttonlist.h"
#include <QScroller>
#include <QDebug>

MainButtonList::MainButtonList(QListWidget *parent)
    : QListWidget{parent}
{
    setupUI();
    applyStyles();
    setCurrentRow(0);
}

void MainButtonList::setupUI() {
    setFixedWidth(52);
    setContentsMargins(4, 4, 4, 4);
    setViewMode(QListView::IconMode);
    setIconSize(QSize(22, 22));
    setGridSize(QSize(52, 60));
    setFlow(QListView::TopToBottom);
    setWrapping(false);
    setFocusPolicy(Qt::NoFocus);
    setResizeMode(QListView::Adjust);
    // setSpacing(4);

    setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    QScroller::grabGesture(this, QScroller::TouchGesture);
    setUniformItemSizes(false);

}

void MainButtonList::applyStyles(){
    setStyleSheet(
        /* 选中状态样式 */
        R"(
        QListWidget {
            font: 12px bold;
            background: #eeeeee;
            border: 0px;
        }
        QListWidget::item {
            border-radius: 5px;
            padding: 6px 0px;
        }
        QListWidget::item:selected {
            outline: 0px;
            background: #8080ee;
            color: white;
        }
        /* 悬停效果 */
        QListWidget::item:hover {
            background: #e0e0ff;
        })"
        );
}

void MainButtonList::SetContent(QList<MainButtonContent> &list) {
    for(auto item : list) {
        QListWidgetItem* button = new QListWidgetItem(QIcon(item.icon), item.text);
        qDebug() << width();
        button->setSizeHint(QSize(52, 56));
        button->setTextAlignment(Qt::AlignCenter);
        button->setData(Qt::UserRole, QVariant::fromValue(item.page));
        addItem(button);
    }
    setCurrentRow(0);
}
